package com.yuju.controller.hotel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.PriceSchemeReturnCodeEnum;
import com.yuju.hotel.model.PriceDetailModel;
import com.yuju.hotel.model.PriceSchemeModel;
import com.yuju.hotel.service.IPriceSchemeService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.PriceSchemeVO;


/**
 * 
 * 类名：PriceSchemeController  <br />
 *
 * 功能：价格方案相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月29日 下午3:43:36  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("priceScheme")
public class PriceSchemeController extends BaseController<PriceSchemeVO, PriceSchemeModel> {

	@Autowired
	private IPriceSchemeService priceSchemeService;
	/**
	 * 
	 * 功能：价格方案列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午2:24:27 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		PriceSchemeVO  priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		Result result = new Result();
		List<Map<String, Object>> list = priceSchemeService.getPriceSchemeList(convert(priceSchemeVO));
		
		if (Utils.isNotNull(list)) {
			result.setData(list);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_QUERY_SUCCESS.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：价格方案保存<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午2:24:41 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		Result result = new Result();
		//映射前端传来的数据
		PriceSchemeVO priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		JSONArray jsonArray = obj.getJSONArray("priceDetailModels");
		
		//将array数组转换成字符串  
		String js = jsonArray.toJSONString();
		//把字符串转换成集合  
		List<PriceDetailModel> priceDetailModels = (List<PriceDetailModel>) JSONObject.parseObject(js,  new TypeReference<ArrayList<PriceDetailModel>>() {});
		
		//添加操作
		int rows = priceSchemeService.savePriceScheme(convert(priceSchemeVO) ,priceDetailModels);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_SAVE_SUCCESS.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_SAVE_FAILURE.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：价格方案更新<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午2:24:55 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		Result result = new Result();
		//映射前端传来的数据
		PriceSchemeVO priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		JSONArray jsonArray = obj.getJSONArray("priceDetailModels");
				
		//将array数组转换成字符串  
		String js = jsonArray.toJSONString();
		//把字符串转换成集合  
		List<PriceDetailModel> priceDetailModels = (List<PriceDetailModel>) JSONObject.parseObject(js,  new TypeReference<ArrayList<PriceDetailModel>>() {});
				
		int rows = priceSchemeService.updatePriceScheme(convert(priceSchemeVO),priceDetailModels);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_UPDATE_FAILURE.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：根据ID查询信息<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午4:43:18 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		PriceSchemeVO  priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		Result result = new Result();
		PriceSchemeModel model = priceSchemeService.findById(priceSchemeVO.getId());
		if (Utils.isNotNull(model)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(model);
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_QUERY_SUCCESS.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：价格方案下拉<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月13日 下午3:12:53 <br/>
	 */
	@RequestMapping("getPriceSchemeDropdown")
	@ResponseBody
	public Result getPriceSchemeDropdown(@RequestBody JSONObject obj) {
		PriceSchemeVO  priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		Result result = new Result();
		List<PriceSchemeModel> list = priceSchemeService.list(convert(priceSchemeVO));
		if (Utils.isNotNull(list)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(list);
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_QUERY_SUCCESS.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：重名验证<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 上午11:24:17 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		PriceSchemeVO  priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		Result result = new Result();
		boolean selectByName = priceSchemeService.selectByName(convert(priceSchemeVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：换房页面  根据换房类型 和 客源类型 价格方案联动<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月20日 下午1:45:06 <br/>
	 */
	@RequestMapping("priceSchemeDropdownList")
	@ResponseBody
	public Result priceSchemeDropdownList(@RequestBody JSONObject obj) {
		PriceSchemeVO  priceSchemeVO = obj.toJavaObject(PriceSchemeVO.class);
		Result result = new Result();
		List<PriceSchemeModel> list = priceSchemeService.priceSchemeDropdownList(convert(priceSchemeVO));
		if (Utils.isNotNull(list)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(list);
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_VALID_SUCCESS.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_VALID_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(PriceSchemeReturnCodeEnum.PRICESCHEME_VALID_FAILURE.getCode());
			result.setReturnMsg(PriceSchemeReturnCodeEnum.PRICESCHEME_VALID_FAILURE.getDesc());
		}
		return result;
	}
}